local BanService = require "modules.ban.BanService"
local BanConst = require "modules.ban.BanConst"
local PlayerService = require "modules.player.PlayerService"
local TimeUtil = require "core.TimeUtil"

local M = {}

function M.banPlayer(params)
    local playerId = params.playerId
    local endTime = params.endTime
    local BAN_RESULT_CODE = BanConst.BAN_RESULT_CODE
    if endTime <= TimeUtil.now() then
        return {
            code = BAN_RESULT_CODE.TIME_TO_EARLY,
            msg = 'time <= now'
        }
    end

    if not PlayerService:isPlayerExist(playerId) then
        return {
            code = BAN_RESULT_CODE.PLAYER_NOT_EXIST,
            msg = 'player not exist'
        }
    end

    local banResult = BanService:banPlayer(playerId, endTime, params.reason, params.desc)
    if banResult == false then
        return {
            code = BAN_RESULT_CODE.BAN_FAIL,
            msg = 'ban fail'
        }
    end

    return {
        code = BAN_RESULT_CODE.SUCCESS
    }
end

function M.unbanPlayer(params)
    BanService:unbanPlayer(params.playerId)
    return {
        code = 1
    }
end

function M.banIP(params)
    local ip = params.ip
    local endTime = params.endTime
    local BAN_RESULT_CODE = BanConst.BAN_RESULT_CODE
    if endTime <= TimeUtil.now() then
        return {
            code = BAN_RESULT_CODE.TIME_TO_EARLY,
            msg = 'time <= now'
        }
    end

    local banResult = BanService:banIP(ip, endTime, params.reason, params.desc)
    if banResult == false then
        return {
            code = BAN_RESULT_CODE.BAN_FAIL,
            msg = 'ban fail'
        }
    end

    return {
        code = BAN_RESULT_CODE.SUCCESS
    }
end

function M.unbanIP(params)
    BanService:unbanIP(params.ip)
    return {
        code = 1
    }
end

return M
